(function($) {
    var placeholderfriend = {
        focus: function(s) {
            s = $(s).hide().prev().show().focus();
            var idValue = s.attr("id");
            if (idValue) {
                s.attr("id", idValue.replace("placeholderfriend", ""));
            }
            var clsValue = s.attr("class");
            if (clsValue) {
                s.attr("class", clsValue.replace("placeholderfriend", ""));
            }
        }
    }

    //判断是否支持placeholder
    function isPlaceholer() {
        var input = document.createElement('input');
        return "placeholder" in input;
    }
    //不支持的代码
    if (!isPlaceholer()) {
        $(function() {

            var form = $(this);
            var elements = form.find("input[type='text'][placeholder]");
            elements.each(function() {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (pValue) {
                    if (sValue == '') {
                        s.val(pValue);
                    }
                }
            });

            elements.focus(function() {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (sValue && pValue) {
                    if (sValue == pValue) {
                        s.val('');
                    }
                }
            });

            elements.blur(function() {
                var s = $(this);
                var pValue = s.attr("placeholder");
                var sValue = s.val();
                if (!sValue) {
                    s.val(pValue);
                }
            });
            /* 对password框的特殊处理
             * 1.创建一个text框
             * 2.获取焦点和失去焦点的时候切换
             */
            $('input[type="password"]').each(function(){
                var pwdField    = $(this);
                var pwdVal      = pwdField.attr('placeholder');
                var pwdId       = pwdField.attr('id');

                // 重命名该input的id为原id后跟1

                pwdField.after('<input id="' + pwdId +'1" type="text" value='+pwdVal+' autocomplete="off" />');
                var pwdPlaceholder = $('#' + pwdId + '1');
                pwdPlaceholder.css({"position":"absolute","left":"0"});

                pwdPlaceholder.show();
                pwdField.hide();
                pwdPlaceholder.focus(function(){
                    pwdPlaceholder.hide();
                    pwdField.show();
                    pwdField.focus();
                });
                pwdField.blur(function(){
                    if(pwdField.val() == '') {
                        pwdPlaceholder.show();
                        pwdPlaceholder.addClass('ac');
                        pwdField.hide();
                    }
                });
            });

        });
    }
    window.placeholderfriendfocus = placeholderfriend.focus;
})(jQuery);